/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.compra.managed;

import java.util.List;
import javax.faces.bean.ManagedBean;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.clases.others.FormularioName.COMPRA.Mtto;
import sv.com.intesal.compra.bean.Linea;
import sv.com.intesal.compra.controlador.LineaControlador;

/**
 *
 * @author ejerez
 */
@ManagedBean(name = "lineaManaged")
public class LineaManaged extends ViewScopedAbstract {

    private Linea selectedLinea;
    private List<Linea> lineas;

    public LineaManaged() {
        this.selectedLinea = new Linea();
        this.lineas = null;
    }

    public void fillGrid() {
        try {
            if (this.lineas == null) {
                if (sm.validar_rol_usuario(Mtto.wfrmLinea, sm.buscar) == true) {
                    LineaControlador mc = new LineaControlador();
                    this.lineas = mc.getLineas();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                if (this.selectedLinea.getCodigo_l() == null) {
                    if (sm.validar_rol_usuario(Mtto.wfrmLinea, sm.agregar) == true) {
                        LineaControlador mc = new LineaControlador();
                        mc.insert(this.selectedLinea);
                        m.messageInfo(m.getIinformation());
                        lm.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario(Mtto.wfrmLinea, sm.modificar) == true) {
                        LineaControlador mc = new LineaControlador();
                        mc.update(this.selectedLinea);
                        m.messageInfo(m.getImodificada());
                        lm.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;

            if (this.selectedLinea.getNombre_l().equals("")) {
                m.messageAdvertencia(m.campoError("NOMBRE"));
                var_return = false;
            }

            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Linea> getLineas() {
        return lineas;
    }

    public void setLineas(List<Linea> lineas) {
        this.lineas = lineas;
    }

    public Linea getSelectedLinea() {
        if (selectedLinea == null) {
            selectedLinea = new Linea();
        }
        return selectedLinea;
    }

    public void setSelectedLinea(Linea selectedLinea) {
        this.selectedLinea = selectedLinea;
    }
}
